﻿<UserControl x:Class="Adan.Client.Plugins.GroupWidget.GroupWidgetControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:Properties="clr-namespace:Adan.Client.Plugins.GroupWidget.Properties"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="300"
             Focusable="False"
             Background="{DynamicResource WidgetBackgroundBrush}"
             MinHeight="22"
             Width="{Binding Width}">
    <UserControl.Resources>
        <ResourceDictionary Source="Resources.xaml"/>
    </UserControl.Resources>
    <StackPanel>
        <ListBox ItemsSource="{Binding GroupMates}"
                 Focusable="False"
                 SelectedValue="{Binding SelectedGroupMate}"
                 AlternationCount="2">
            <ListBox.Template>
                <ControlTemplate TargetType="{x:Type ListBox}">
                    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </ControlTemplate>
            </ListBox.Template>
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="SnapsToDevicePixels"
                            Value="true" />
                    <Setter Property="OverridesDefaultStyle"
                            Value="true" />
                    <Setter Property="Focusable"
                            Value="True" />
                    <EventSetter Event="PreviewMouseDown"
                                 Handler="CancelFocusingListBoxItem" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                <Border BorderThickness="0,0,0,2">
                                    <StackPanel Background="{TemplateBinding Background}" Orientation="Horizontal">
                                        <Grid>
                                            <Border Width="5" VerticalAlignment="Stretch" Background="Black" Name="PART_NotSelectedBorder"/>
                                            <Border Width="3" VerticalAlignment="Stretch" Background="White" Name="PART_SelectedBorder"/>
                                        </Grid>
                                        <ContentPresenter HorizontalAlignment="Stretch"
                                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                      Margin="0"
                                                      x:Name="contentPresenter" />
                                    </StackPanel>
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter Property="Visibility" TargetName="PART_SelectedBorder" Value="Visible"/>
                                    </Trigger>
                                    <Trigger Property="IsSelected" Value="False">
                                        <Setter Property="Visibility" TargetName="PART_SelectedBorder" Value="Collapsed"/>
                                    </Trigger>

                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                            <Setter Property="Background" Value="Black">
                            </Setter>
                        </Trigger>
                        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                            <Setter Property="Background">
                                <Setter.Value>
                                    <SolidColorBrush Color="#FF1B1B1B"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>

                    </Style.Triggers>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Height="20"
                                Name="ItemStack"
                                HorizontalAlignment="Stretch"
                                Orientation="Horizontal">
                        <TextBlock Text="{Binding Number, StringFormat=\{0\}\.}"
                                   Width="22"
                                   FontSize="11pt"
                                   FontFamily="Lucida Console"
                                   VerticalAlignment="Bottom"
                                   HorizontalAlignment="Right"
                                   Foreground="{DynamicResource GlyphBrush}"
                                   Visibility="{Binding DisplayNumber, Converter={StaticResource BooleanToVisibilityConverter}}"
                                   Margin="0,0,0,2"
                                    />
                        <TextBlock Text="{Binding Name}"
                                   FontSize="12pt"
                                   Width="140"
                                   FontFamily="Lucida Console"
                                   VerticalAlignment="Bottom"
                                   HorizontalAlignment="Left"
                                   TextTrimming="CharacterEllipsis"
                                   Foreground="{Binding HitsColor, Converter={StaticResource BackgroundTextColorToBrushConverter}}" 
                                   MaxWidth="135"
                                   Margin="0,0,0,2"/>
                        <Border Margin="1,0,1,0" 
                                Width="2"
                                Background="DarkOrange"
                                VerticalAlignment="Bottom"
                                Height="{Binding WaitTimeHeight}"/>
                        <Grid Width="60">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="14" />
                                <RowDefinition Height="6" />
                            </Grid.RowDefinitions>
                            <Border Width="60"
                                    HorizontalAlignment="Left"
                                    Height="14"
                                    Grid.Row="0"
                                    Background="{Binding HitsColor, Converter={StaticResource BackgroundTextColorToBrushConverter}}"
                                    Panel.ZIndex="0" />
                            <Border Width="{Binding Converter={StaticResource PercentageConverter}, ConverterParameter=60, Path=HitsPercent}"
                                    HorizontalAlignment="Left"
                                    Height="14"
                                    Grid.Row="0"
                                    Panel.ZIndex="1">
                                <Border.Background>
                                    <LinearGradientBrush EndPoint="0.5,1"
                                                         StartPoint="0.5,0">
                                        <GradientStop Color="#FF226E01" />
                                        <GradientStop Color="White"
                                                      Offset="0.5" />
                                        <GradientStop Color="#FF226E01"
                                                      Offset="1" />
                                    </LinearGradientBrush>
                                </Border.Background>
                            </Border>
                            <TextBlock Text="{Binding HitsPercent, StringFormat=\{0\,4:##0\\%\}}"
                                       VerticalAlignment="Center"
                                       HorizontalAlignment="Center"
                                       Foreground="Black"
                                       FontFamily="Lucida Console"
                                       FontSize="11"
                                       Grid.Row="0"
                                       Panel.ZIndex="1" />
                            <Border Width="60"
                                    HorizontalAlignment="Left"
                                    Height="6"
                                    Grid.Row="1"
                                    Background="{Binding MovesColor, Converter={StaticResource BackgroundTextColorToBrushConverter}}"
                                    Panel.ZIndex="0" />
                            <Border Width="{Binding Converter={StaticResource PercentageConverter}, ConverterParameter=60, Path=MovesPercent}"
                                    HorizontalAlignment="Left"
                                    Height="6"
                                    Grid.Row="1"
                                    Panel.ZIndex="1">
                                <Border.Background>
                                    <LinearGradientBrush EndPoint="0.5,1"
                                                         StartPoint="0.5,0">
                                        <GradientStop Color="#FFBA0202" />
                                        <GradientStop Color="White"
                                                      Offset="0.5" />
                                        <GradientStop Color="#FFBA0202"
                                                      Offset="1" />
                                    </LinearGradientBrush>
                                </Border.Background>
                            </Border>
                        </Grid>
                        <Image Style="{StaticResource SmallIcon}"
                               Margin="5,0,0,0"
                               SnapsToDevicePixels="True"
                               Source="{Binding Position, Converter={StaticResource PositionToIconConverter}, NotifyOnTargetUpdated=True}">
                        </Image>
                        <Image Style="{StaticResource SmallIcon}"
                               SnapsToDevicePixels="True"
                               Source="{Binding IsAttacked, Converter={StaticResource AttackedToIconConverter}, NotifyOnTargetUpdated=True}">
                        </Image>
                        <ItemsControl ItemsSource="{Binding Path=AffectsSortedAndFiltered}"
                                      VerticalAlignment="Stretch"
                                      Width="{Binding AffectsPanelWidth}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal" />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <DataTemplate.Triggers>
                                        <DataTrigger Binding="{Binding IsBlinking}"
                                                     Value="true">
                                            <DataTrigger.EnterActions>
                                                <BeginStoryboard Name="BlinkingAnimation">
                                                    <Storyboard>
                                                        <DoubleAnimation From="1"
                                                                         To="0"
                                                                         RepeatBehavior="Forever"
                                                                         AutoReverse="True"
                                                                         Duration="0:0:0.1"
                                                                         Storyboard.TargetProperty="Opacity"
                                                                         Storyboard.TargetName="AffectImage" />
                                                    </Storyboard>
                                                </BeginStoryboard>
                                            </DataTrigger.EnterActions>
                                            <DataTrigger.ExitActions>
                                                <StopStoryboard BeginStoryboardName="BlinkingAnimation" />
                                            </DataTrigger.ExitActions>
                                        </DataTrigger>
                                    </DataTemplate.Triggers>
                                    <Grid Width="20"
                                          Height="20"
                                          Margin="3,0,0,0">
                                        <Grid.ToolTip>
                                            <StackPanel Orientation="Horizontal">
                                                <Image Style="{StaticResource MediumIcon}"
                                                       SnapsToDevicePixels="True"
                                                       Source="{Binding DisplayIcon, Converter={StaticResource AffectToIconConverter}, NotifyOnTargetUpdated=True}" />
                                                <Grid Margin="5,0,0,0">
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition />
                                                        <ColumnDefinition />
                                                    </Grid.ColumnDefinitions>
                                                    <Grid.RowDefinitions>
                                                        <RowDefinition />
                                                        <RowDefinition />
                                                    </Grid.RowDefinitions>
                                                    <TextBlock Text="{x:Static Properties:Resources.Affect}"
                                                               Grid.Row="0"
                                                               Grid.Column="0" />
                                                    <TextBlock Text="{x:Static Properties:Resources.Duration}"
                                                               Grid.Row="1"
                                                               Grid.Column="0" />
                                                    <TextBlock Text="{Binding RealAffectName}"
                                                               Grid.Row="0"
                                                               Grid.Column="1"
                                                               Margin="5,0,0,0" />
                                                    <TextBlock Text="{Binding SecondsLeft, Converter={StaticResource SecondsToTimeSpanConverter}, StringFormat=\{0:mm\\:ss\}}"
                                                               Visibility="{Binding AffectDescription.IsRoundBased, Converter={StaticResource InverseBooleanToVisibilityConverter}}"
                                                               Grid.Row="1"
                                                               Grid.Column="1"
                                                               Margin="5,0,0,0" />
                                                    <TextBlock Text="{Binding StringFormat=\{0\} round(s), Path=RoundsLeft}"
                                                               Visibility="{Binding AffectDescription.IsRoundBased, Converter={StaticResource BooleanToVisibilityConverter}}"
                                                               Grid.Row="1"
                                                               Grid.Column="1"
                                                               Margin="5,0,0,0" />
                                                </Grid>
                                            </StackPanel>
                                        </Grid.ToolTip>
                                        <Image SnapsToDevicePixels="True"
                                               Source="{Binding DisplayIcon, Converter={StaticResource AffectToIconConverter}, NotifyOnTargetUpdated=True}"
                                               HorizontalAlignment="Right"
                                               Name="AffectImage"
                                               Style="{StaticResource SmallIcon}" />

                                        <TextBlock Text="{Binding SecondsLeft, StringFormat=\{0:#\}}"
                                                   HorizontalAlignment="Right"
                                                   VerticalAlignment="Bottom"
                                                   FontSize="10"
                                                   Foreground="Yellow"
                                                   Visibility="{Binding SecondsLeftVisible, Converter={StaticResource BooleanToVisibilityConverter}}" />
                                        <Border HorizontalAlignment="Right"
                                                VerticalAlignment="Bottom"
                                                Width="4"
                                                Height="{Binding RoundsLeft, Converter={StaticResource MultiplierConverter}, ConverterParameter=4}"
                                                Background="{StaticResource RoundIndicatorBrush}"
                                                Visibility="{Binding RoundsLeftVisible, Converter={StaticResource BooleanToVisibilityConverter}}" />
                                    </Grid>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                        <Image Style="{StaticResource SmallIcon}"
                               SnapsToDevicePixels="True"
                               Source="{Binding InSameRoom, Converter={StaticResource InSameRoomToIconConverter}, NotifyOnTargetUpdated=True}"
                               HorizontalAlignment="Right" />
                        <TextBlock Text="{Binding MemTime}"
                                   Width="40"
                                   FontSize="10pt"
                                   FontFamily="Lucida Console"
                                   VerticalAlignment="Center"
                                   HorizontalAlignment="Center"
                                   Foreground="{DynamicResource GlyphBrush}"
                                   Margin="4,0,0,2"
                                    />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Border Visibility="{Binding MoreItemsAvailable, Converter={StaticResource BooleanToVisibilityConverter}}" 
                HorizontalAlignment="Stretch" 
                Height="10">
            <TextBlock 
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Foreground="{DynamicResource GlyphBrush}"
                Text="..."/>
        </Border>
    </StackPanel>
</UserControl>
